package com.lagou.dao.impl;

import com.lagou.dao.CourseDao;
import com.lagou.pojo.Course;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CourseDaoImpl implements CourseDao {
    //    重写方法

    @Override
    public List<Course> findCourseList() {
        try {
//   1.     查询数据库  query ()查询  update （）更新删除 添加
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
//       2.sql 语句
            String sql="select id,course_name,price,STATUS,sort_num from course where is_del= ? ";

//        qureu 查询 对象形式

            List<Course> courseList = qr.query(sql, new BeanListHandler<Course>(Course.class),0);
            return courseList;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }
    /*
     *  根据课程名称和状态查询
     * */
    @Override
    public List<Course> findCourseNameAndStatus(String course_name,String status) {

        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            // sql
            StringBuffer sb = new StringBuffer("select id,course_name,price,status,sort_num from course where is_del = ?");
            List<Object> list = new ArrayList<>();
            list.add(0); //[0]
            if(course_name!=null){
                sb.append(" and course_name like ?");
                //%course_name%
                course_name = "%"+course_name+"%";
                list.add(course_name); //[0,"课程内容"]
                System.out.println(sb);

            }else if(status!=null){
                sb.append("and status = ?");
                int i = Integer.parseInt(status);
                list.add(i); // [0,"微服务",1]
            }

            //query
            List<Course> result = qr.query(sb.toString(), new BeanListHandler<Course>(Course.class), list.toArray());
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
  /*
  * 创建课程
  *
  * */


    @Override
    public int saveCourseSalesInfo(Course course) {
        try {
            //1.创建QueryRunner
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            //2.编写SQL
            String sql = "INSERT INTO course(\n" +
                    "course_name,\n" +
                    "brief,\n" +
                    "teacher_name,\n" +
                    "teacher_info,\n" +
                    "preview_first_field,\n" +
                    "preview_second_field,\n" +
                    "discounts,\n" +
                    "price,\n" +
                    "price_tag,\n" +
                    "share_image_title,\n" +
                    "share_title,\n" +
                    "share_description,\n" +
                    "course_description,\n" +
                    "course_img_url,\n" +
                    "STATUS,\n" +
                    "create_time,\n" +
                    "update_time\n" +
                    ")VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";

            //3.准备参数
            Object[] param = {course.getCourse_name(),course.getBrief(),course.getTeacher_name(),course.getTeacher_info(),
                    course.getPreview_first_field(),course.getPreview_second_field(),course.getDiscounts(),course.getPrice(),
                    course.getPrice_tag(),course.getShare_image_title(),course.getShare_title(),course.getShare_description(),
                    course.getCourse_description(),course.getCourse_img_url(),course.getStatus(),course.getCreate_time(),course.getUpdate_time()};

            //4.执行插入操作
            int row = qr.update(sql, param);

            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }


    //获得id

    @Override
    public List<Course> findCourseById(int id) {
        try {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        //update(sql,params)
        // query(sql,new BeanListHandler<Object>(object.class),params)

        //sql
       String sql = "select  \n" +
               "id, \n" +
               "course_name,\n" +
               "teacher_info,\n" +
               "teacher_name,\n" +
               "preview_first_field,\n" +
               "preview_second_field,\n" +
               "discounts,\n" +
               "price,\n" +
               "price_tag,\n" +
               "share_image_title,\n" +
               "share_title,\n" +
               "share_description,\n" +
               "course_description,\n" +
               "STATUS\n" +
               "from course where id = ?;";

       //query

            List<Course> list = qr.query(sql, new BeanListHandler<Course>(Course.class), id);
        return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int UpdateCourseInfo(Course course) {
        try {    QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
//
            String sql = "UPDATE course SET \n" +
                    "course_name = ?,\n" +
                    "brief = ?,\n" +
                    "teacher_name = ?,\n" +
                    "teacher_info = ?,\n" +
                    "preview_first_field = ?,\n" +
                    "preview_second_field = ?,\n" +
                    "discounts = ?,\n" +
                    "price = ?,\n" +
                    "price_tag = ?,\n" +
                    "share_image_title = ?,\n" +
                    "share_title = ?,\n" +
                    "share_description = ?,\n" +
                    "course_description = ?,\n" +
                    "course_img_url = ?,\n" +
                    "update_time = ?\n" +
                    "WHERE id = ?";


            Object[] params={
                    course.getCourse_name(),course.getBrief(),course.getTeacher_name(),course.getTeacher_info(),
                    course.getPreview_first_field(),course.getPreview_second_field(),course.getDiscounts(),course.getPrice(),course.getPrice_tag(),
                    course.getShare_image_title(),course.getShare_title(),course.getShare_description(),course.getCourse_description(),
                    course.getCourse_img_url(),course.getUpdate_time(),course.getId()

            };

            int row = qr.update(sql, params);
            return  row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }


    }
}
